home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / Node < prev    next >
Text File  |  2003-02-14  |  3KB  |  91 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "Node.h"
  39.  
  40. DEFINE_RTTI_BASE(Node);
  41.  
  42. Node::~Node()
  43. {
  44.  if (parent) parent->removeChild(this);
  45.  while (lastChild) delete removeChild(lastChild);
  46. }
  47.  
  48. //******************************************************************
  49.  
  50. bool Node::hasChildNodes()                {return 0;}
  51.  
  52. //******************************************************************
  53.  
  54. Node* Node::addChild(Node* n)
  55. {
  56.   if (n==0 || !hasChildNodes()) return 0;
  57.   n->parent=this;
  58.   n->next=0;
  59.   n->previous=lastChild;
  60.  
  61.   if (lastChild) 
  62.      lastChild->next=n;
  63.   else 
  64.      firstChild=n;
  65.  
  66.   lastChild=n;
  67.   return n;
  68. }
  69.  
  70. //******************************************************************
  71.  
  72. Node* Node::removeChild(Node* n)
  73. {
  74.   if (n==0 || n->parent!=this) return 0;
  75.  
  76.   if (n->previous) 
  77.       n->previous->next=n->next;
  78.   else
  79.       firstChild=n->next;
  80.  
  81.   if (n->next) 
  82.       n->next->previous=n->previous;
  83.   else
  84.       lastChild=n->previous;
  85.  
  86.   n->parent=0;
  87.   n->next=0;
  88.   n->previous=0;
  89.   return n;
  90. }
  91.